const {
  http
} = require("../../utils/apiutils")

Page({
  /**
   * 页面的初始数据
   */
  data: {
    type: "",
    id: "",
  },
  //登录系统
  loginsys() {
    var data = {
      "phone": "17319743876", //手机号码
      "password": "a123456" //密码
    }
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    http.authenticate(data,
      function (res) {
        wx.hideLoading()
        // console.log("------用户登录信息哦---", res);
        if (res.code == 0) {
          getApp().globalData.exist = res.exist;
          getApp().globalData.token = res.token;
        } else {
          wx.showToast({
            title: res.message,
            icon: "none",
          })
        }
      },
      function (err) {
        wx.hideLoading()
      })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  //获取用户信息
  getuser: function () {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    // console.log('getUser',getApp().globalData.route)
    http.userInfo({},
      function (res) {
        wx.hideLoading()
        console.log("------用户信息--", res);
        if (res.code == 0) {
          getApp().globalData.userInfo = res.data;
          // console.log('getUser',getApp().globalData.route)
          if (getApp().globalData.route) {
            // console.log('进入route')
            let url = ""
            switch (getApp().globalData.route) {
                case 'searchgoods':
                    url = '/pages/home/pages/searchgoods/searchcar'
                    break;
                case 'searchcar':
                    url = '/pages/home/pages/searchcar/searchcar'
                    break;
                case 'searchGDtl':
                    url = '/pages/home/pages/searchGDtl/index?' + getApp().globalData.routeParams
                    break;
                case 'activity':
                    url = '/pages/home/pages/activity/activity?' + getApp().globalData.routeParams
                    break;
                case 'information':
                    url = '/pages/tabBar/information/index'
                    break;
                case 'list-dtl':
                    url = '/pages/information/pages/list-dtl/index?'+getApp().globalData.routeParams
                    break;
                case 'work':
                    url = '/pages/home/pages/work/work?'+getApp().globalData.routeParams
                    break;
            }
            wx.redirectTo({
                url: url,
                success() {
                    getApp().globalData.route = ""
                },
                fail(err) {
                    console.log(err)
                }
            })
        }else if (that.data.type == "login") {
            wx.switchTab({
              url: '/pages/tabBar/home/index',
            })
          } else if (that.data.type == "work") {
            wx.redirectTo({
              url: '/pages/home/pages/work/work?id=' + that.data.id,
            })
          } else {
            wx.switchTab({
              url: '/pages/tabBar/home/index',
            })
          }
        } else {
          wx.showToast({
            title: res.message,
            icon: "none",
          })
        }
      },
      function (err) {
        wx.hideLoading()
      })
  },
  wclogin: function () {
    var that = this;
    wx.login({ //调用获取用户openId
      success: function (res) {
			// console.log(res)
        that.wxlogin(res.code)
      }
    })
  },
  wxlogin: function (code) {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    http.weChatLogin({
        code: code
      },
      function (res) {
        wx.hideLoading()
        // console.log("------用户信息--", res);
        if (res.code == 0) {
          getApp().globalData.exist = res.exist;
          getApp().globalData.token = res.token;
          that.getuser();
        } else if (res.code == 1 || res.code == 2) {
          getApp().globalData.openid = res.openid;
          wx.navigateTo({
            url: '/pages/login/login',
          })
        }else {
          wx.showToast({
            title: res.message,
            icon: "none",
          })
        }
      },
      function (err) {
        wx.hideLoading()
      })
  },
	onLoad: function (options) {
    // console.log('进入时携带的参数',options)
    if(options.rId){
      getApp().globalData.share_id = options.rId;
    }
    if(options.scene){
      if(this.getScene(options.scene).rId){
        getApp().globalData.share_id =  this.getScene(options.scene).rId;
      } 
      if (this.getScene(options.scene).route == 'searchgoods') {
        // 公益列表跳转
        getApp().globalData.route = this.getScene(options.scene).route;
      }
      if (this.getScene(options.scene).route == 'searchGDtl') {
        // 公益跳转
        getApp().globalData.route = this.getScene(options.scene).route;
        getApp().globalData.routeParams = 'id=' + this.getScene(options.scene).dtlId
      }
      if (this.getScene(options.scene).route == 'work') {
        // 公益跳转
        getApp().globalData.route = this.getScene(options.scene).route;
        getApp().globalData.routeParams = 'id=' + this.getScene(options.scene).id
      }
    }
    if(options.route){
      if (options.route == 'searchGDtl') {
        getApp().globalData.route = options.route;
        getApp().globalData.routeParams = 'id=' + options.dtlId;
        getApp().globalData.inviteCode = options.code;
      }
      if (options.route == 'searchgoods') {
        getApp().globalData.route = options.route
      }
    }
    this.wclogin()
    this.setData({
      userInfo:getApp().globalData.userInfo,
    })
    
    wx.getSystemInfo({
      success: (e) => {
        // console.log("-----状态栏高度------", e.statusBarHeight);
        let capsule = wx.getMenuButtonBoundingClientRect();
        // console.log("-----------", capsule);
        if (capsule) {
          getApp().globalData.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight;
        } else {
          getApp().globalData.CustomBar = e.statusBarHeight + 50;
        }
        getApp().globalData.statusBar = e.statusBarHeight;
      },

    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var shareObj = {
      title: "分享岗位", // 默认是小程序的名称(可以写slogan等)
      path: '/pages/index/index', // 
      imageUrl: '',
      success: function (res) {
        // 转发成功之后的回调
        if (res.errMsg == 'shareAppMessage:ok') {}
      }
    }
    console.log("------------", shareObj);
    // 返回shareObj
    return shareObj;
  },
  getScene(scene = "") {
    console.log('扫描出的参数',scene)
    if (scene == "") return null;
    let res = {};
    let res1={};

    let params =  params =decodeURIComponent(scene).split("&");
    params.forEach(item => {
      let pram=[]
      console.log()
      if(item.indexOf(':')>0){
        console.log(1)
        pram = item.split(":")
      }else{
        console.log(2)
        pram = item.split("=")
      }
      res[pram[0]] = pram[1]
    })
    console.log('老的params',params)
    console.log('老的params',res)
    return res
  }
})